﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-4-18
/// Purpose: 		可配置的路由表注册类定义文件
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2012-12-14      当设置为禁用路由时，不在路由表中增加值
/// ****************************************************************************
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Routing;
using System.Web.Mvc;
using System.Collections.Specialized;
using System.Collections.ObjectModel;

namespace RB.Web.Mvc
{
    /// <summary>
    /// 可配置的路由表注册类
    /// </summary>
    public static class ConfigurableRoutes
    {
        public static void Register()
        {
            var routes = RouteTable.Routes;

            RoutesConfigSection config = (RoutesConfigSection)ConfigurationManager.GetSection("RB.RouteTable");

            // 禁用Route
            if (config.Disabled)
                return;

            if (!string.IsNullOrEmpty(config.IgnoreRoute))
            {
                routes.Ignore(config.IgnoreRoute);
            }

            foreach (RouteConfigElement routeConfig in config.Routes)
            {
                Type routeType = Type.GetType(routeConfig.Type);
                string url = routeConfig.Url;
                RouteValueDictionary values = new RouteValueDictionary();
                Collection<string> passThroughValues = new Collection<string>();
                NameValueCollection cookieNames = new NameValueCollection();
                foreach (RouteDefaultValueConfigElement defaultValue in routeConfig.DefaultValues)
                {
                    if (defaultValue.Optional)
                    {
                        values.Add(defaultValue.Key, UrlParameter.Optional);
                    }
                    else if (defaultValue.PassThrough)
                    {
                        passThroughValues.Add(defaultValue.Key);
                        if (!string.IsNullOrEmpty(defaultValue.CookieName))
                        {
                            cookieNames.Add(defaultValue.Key, defaultValue.CookieName);
                        }
                    }
                    else
                    {
                        values.Add(defaultValue.Key, defaultValue.Value);
                    }
                }
                IRouteHandler handler = new MvcRouteHandler();
                Route route = (Route)Activator.CreateInstance(routeType, url, values, handler);
                ConfigurableRoute configurableRoute = route as ConfigurableRoute;
                if (configurableRoute != null)
                {
                    configurableRoute.Area = routeConfig.Area;
                    configurableRoute.PassThroughParameters = passThroughValues.ToArray();

                    configurableRoute.AutoTimestampParameter = routeConfig.AutoTimestampParameter;

                    configurableRoute.PassThroughParameterCookieNames = cookieNames;
                }

                routes.Add(route);
            }
        }
    }
}
